package controllers.api;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import models.InternshipFamiliar;
import models.Photo;
import models.User;
import models.Profile.ProfileType;
import play.mvc.Http.StatusCode;
import play.mvc.Scope.Session;
import utils.constants.SessionConstants;
import controllers.Check;
import controllers.Data;
import controllers.Security;

@Check(Security.USER_ROLE)
public class MeApi extends ApiSecured {
	
	protected static User getCurrentUser() {
		String username = Session.current().get(SessionConstants.USER);
		if(username.contains("@")) username = username.substring(0, username.indexOf("@"));
		User current = User.findByUsername(username);
		return current;
	}
	
	
	public static void familiars(Integer offset, Integer limit) {
		User internship = getCurrentUser();
		if(internship == null) {
			response.status = StatusCode.NOT_FOUND;
			return;
		}
		if(offset == null) offset = 0;
		if(limit==null || limit > LIMIT || limit==0) {
			limit = LIMIT;
		}
		Data<User> data = new Data<User>();
		data.total = InternshipFamiliar.countByInternship(internship);
		data.data = InternshipFamiliar.paginateByInternship(InternshipFamiliar.findByInternship(internship), offset, limit);
		renderTemplate("api/userList.json", data);
	}
	
	public static void internals(Integer offset, Integer limit) {
		User familiar = getCurrentUser();
		if(familiar == null) {
			response.status = StatusCode.NOT_FOUND;
			return;
		}	
		if(offset == null) offset = 0;
		if(limit==null || limit > LIMIT || limit==0) {
			limit = LIMIT;
		}
		Data<User> data = new Data<User>();
		if(familiar.isAdmin()) {
			data.total = User.countInternals();
			data.data = User.getInternals(offset, limit);
			
		} else {
			data.total = InternshipFamiliar.countByFamiliar(familiar);
			data.data = InternshipFamiliar.paginateByFamiliar(InternshipFamiliar.findByFamiliar(familiar), offset, limit);
		}
		renderTemplate("api/userList.json", data);
	}
	
}